<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of haima
 *
 * @author sunday add 2014.7.28  modify by dragon.hu @2016.2.19
 */
require_once 'BaseChannel.php';

//海马
class Haima implements BaseChannel 
{
	
function __construct() {
        //echo "construct";
    }

    //put your code here
    function android($data) {
    	$UID = "";
        $paymentKey = "";        
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
    }

    function ios($data) {
    	$UID = "";
        $paymentKey = "13012e19877c69c4e15c46312b805d89";        
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
    }

	function paymentCallBack($paymentKey, $platform)
	{	
		$orderLog = new ChargeOrder();
		$orderLog->log("haima",$_REQUEST);
		
		$notify_time = $_REQUEST['notify_time'];
		$appid = $_REQUEST['appid'];
		$out_trade_no = $_REQUEST['out_trade_no'];
		$total_fee = $_REQUEST['total_fee'];
		$subject = $_REQUEST['subject'];
		$body = $_REQUEST['body'];
		$trade_status = $_REQUEST['trade_status'];
		$sign = $_REQUEST['sign'];
		$user_param = $_REQUEST['user_param'];
		
		/*var_dump($notify_time);
		var_dump(urlencode($notify_time));
		var_dump($appid);
		var_dump(urlencode($appid));
		var_dump($out_trade_no);
		var_dump(urlencode($out_trade_no));
		var_dump($total_fee);
		var_dump(urlencode($total_fee));
		var_dump($subject);
		var_dump(urlencode($subject));
		var_dump($body);
		var_dump(urlencode($body));
		var_dump($trade_status);
		var_dump(urlencode($trade_status));
		var_dump($user_param);
		var_dump(urlencode($user_param));
		
		var_dump($sign);*/
		
		$mysign = "notify_time=".urlencode($notify_time)."&appid=".urlencode($appid)."&out_trade_no=".urlencode($out_trade_no)."&total_fee=".urlencode($total_fee)."&subject=".urlencode($subject).
		"&body=".urlencode($body)."&trade_status=".urlencode($trade_status).urlencode($paymentKey);
		/*var_dump(md5($mysign));
		$mysigntmp = "notify_time=".$notify_time."&appid=".$appid."&out_trade_no=".$out_trade_no."&total_fee=".$total_fee."&subject=".$subject.
		"&body=".$body."&trade_status=".$trade_status.$paymentKey;
		var_dump(md5($mysigntmp));
		exit();*/
		
		if($sign == md5($mysign))
		{
			//验证通过
			$status = STATUS_SUCCESS;
			if(1 != $trade_status)
			{
				$status = STATUS_FAILD;
			}
			
			//进行发货
			if(false == $orderLog->recordOrder($status, $total_fee, $out_trade_no, "IosHaiMa", $platform))
			{
				$ret="failed";
				$orderLog->log("haima",array("order"=>$out_trade_no,"money"=>$total_fee,"result"=>$ret,"serverresult"=>"faild"));
				//echo " insert order:$ext error ";
			}
			else 
			{
				//支付成功
				$ret="success";
				$orderLog->log("haima",array("order"=>$out_trade_no,"money"=>$total_fee,"result"=>$ret,"serverresult"=>"success"));
			}
		}
		else
		{
			$orderLog->log("haima","  order:$out_trade_no  sign error");
			$ret="failed";
		}
		return $ret;
	}
}